package com.examiner.controller;
import com.examiner.bean.test.TestDB;
import com.examiner.bean.vo.TestDBVO;
import com.examiner.page.QueryInfo;
import com.examiner.result.Result;
import com.examiner.service.TestDBService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;

@RestController
@RequestMapping("/testDB")
public class TestDBController {
    @Resource
    private TestDBService testDBService;

    /**
     * 分页查询
     * @param queryInfo
     * @return
     */
    @PostMapping("/getTestDBList")
    public Result<Object> getTestDBList(@RequestBody QueryInfo queryInfo){
        HashMap<String, Object> data = new HashMap<>();
        List<TestDB> testDBList = testDBService.getTestDBList(queryInfo);
        int total = testDBService.getTestDBTotal(queryInfo.getQuery());
        data.put("total",total);
        data.put("testDBList",testDBList);
        return Result.RESULT(200,"成功获取考评库列表",true,data);
    }

    /**
     * 分页查询
     * @param queryInfo
     * @return
     */
    @PostMapping("/getTestDBList/{id}")
    public Result<Object> getTestDBListByUser(@PathVariable("id")Integer id,@RequestBody QueryInfo queryInfo){
        HashMap<String, Object> data = new HashMap<>();
        System.out.println(id);
        List<TestDB> testDBList = testDBService.getTestDBList(queryInfo,id);
        int total = testDBService.getTestDBByUserTotal(queryInfo.getQuery(),id);
        data.put("total",total);
        data.put("testDBList",testDBList);
        return Result.RESULT(200,"成功获取考评库列表",true,data);
    }

    /**
     * 新增
     * @param testDB
     * @return
     */
    @PostMapping("/addTestDB")
    public Result<Object> addTestDB(@RequestBody TestDBVO testDB){
        try {
            testDBService.save(testDB);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.RESULT(201,"新增考评库内容失败",false,null);
        }
        return Result.RESULT(200,"新增考评库内容成功",true,null);
    }

    /**
     * 根据ID获取考评库内容
     * @param testId
     * @return
     */
    @PostMapping("/getById/{testId}")
    public Result<TestDBVO> getTestById(@PathVariable("testId")Integer testId){
        TestDBVO testDB = testDBService.getTestDbById(testId);
        System.out.println(testDB.toString());
        return  Result.RESULT(200,"获取考评库内容成功",true,testDB);
    }

    /**
     * 删除
     * @param testId
     * @return
     */
    @PostMapping("/deleteTestDB/{testId}")
    public Result<Object> deleteTestDB(@PathVariable("testId")Integer testId){
        try {
            testDBService.deleteTestDB(testId);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.RESULT(201,"删除考评库失败",false,null);
        }
        return Result.RESULT(200,"删除考评库成功",true,null);
    }

    /**
     * 修改考评库内容
     * @param testDBVO
     * @return
     */
    @PostMapping("/updateTestDB")
    public Result<Object> updateTestDB(@RequestBody TestDBVO testDBVO){
        try {
            testDBService.updateTestDB(testDBVO);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.RESULT(201,"修改考评库失败",true,null);
        }
        return Result.RESULT(200,"修改考评库成功",true,null);
    }

    /**
     * 修改考评库内容状态
     * @param testDBId
     * @param testState
     * @return
     */
    @PostMapping("/changeTestState/{testDBId}")
    public Result<Object> changeTestState(@PathVariable("testDBId")Integer testDBId,
                                          @RequestBody Boolean testState){
        try{
            testDBService.changeTestState(testDBId,testState);
        }catch (Exception e){
            e.printStackTrace();
            return Result.RESULT(201,"修改考评库失败",true,null);
        }
        return Result.RESULT(200,"修改考评库状态成功",true,null);
    }



}
